/**
 * 
 */
package com.apanal.qlife.org.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.org.model.Organization;
import com.apanal.qlife.org.service.OrganizationService;
import com.apanal.qlife.sys.model.User;
import com.apanal.qlife.sys.service.UserService;

/**
 * @author Admin
 *
 */
@Controller
@RequestMapping("/organization")
public class OrganizationController {

	@Autowired
	private OrganizationService OrganizationService;
	
	@Autowired
	private UserService service;
	
	/**
	 * 查询平台列表信息列表
	 * @param model
	 * @return
	 */
	@RequestMapping("/organizationlist")
	public String findOrganizationList(Organization organization,Page page,Model model)
	{
		organization.setOrgType(1);
		PageReturn<Organization> result = OrganizationService.findOrganizationList(organization,page);
		model.addAttribute("result",result);
		return "org/organizationList";
	}
	
	/**
	 * 机构下拉框查询
	 * @param organization
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/findOrgList")
	public List<Organization> findOrgList(Organization organization)
	{
		organization.setAuditStatus(2);
		organization.setNormal(1);
		return OrganizationService.findOrgList(organization);
	}
	
	/**
	 * 添加平台信息
	 * @param organization
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="save",method = RequestMethod.POST)
	public long saveOrganization(@CurrentUser User user,Organization organization)
	{
		return OrganizationService.saveOrganization(user,organization);
	}
	
	/**
	 * 跳转到平台添加页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/addOrganization", method = RequestMethod.GET)
	public String addOrganization(Model model)
	{
		Organization organization = new Organization();
		organization.setOrgId(Long.valueOf(1));
		Organization org = OrganizationService.selectByPrimaryKey(organization);
		organization.setOrgPId(org.getOrgId());
		organization.setOrgPIds(org.makeSelfAsParentIds());
		model.addAttribute("org",organization);
		return "org/addOrganization";
	}
	
	/**
	 * 跳转到渠道添加页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/addTrench")
	public String addTrench(Model model)
	{
		return "org/addTrench";
	}
	
	/**
	 * 跳转到物业添加页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/addTenement")
	public String addTenement(Model model)
	{
		
		return "org/addTenement";
	}
	
	/**
	 * 查询渠道列表
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/trenchlist")
	public String findTrenchList(Organization organization, Page page, Model model)
	{
		organization.setOrgType(2);
		PageReturn<Organization> result = OrganizationService.findTrenchList(organization,page);
		model.addAttribute("result",result);
		return "org/trenchList";
	};
	
	/**
	 * 物业查询列表
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/tenementList")
	public String findTenementList(Organization organization, Page page , Model model)
	{
		organization.setOrgType(3);
		PageReturn<Organization> result = OrganizationService.findTenementList(organization,page);
		model.addAttribute("result",result);
		return "org/tenementList";
	}
	
	/**
	 * 根据orgId查询单条平台记录
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/selectById")
	public String selectById(Model model,Organization organization)
	{
		model.addAttribute("org",OrganizationService.selectByPrimaryKey(organization));
		return "org/editOrganization";
	}
	
	/**
	 * 根据orgId查询单条平台记录
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/orgDetails")
	public String orgDetails(Model model,Organization organization)
	{
		model.addAttribute("org",OrganizationService.selectByPrimaryKey(organization));
		return "org/organizationDetails";
	}
	
	/**
	 * 根据orgId查询单条渠道记录
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/selectTrenchById")
	public String selectTrenchById(Model model,Organization organization)
	{
		model.addAttribute("org",OrganizationService.selectByPrimaryKey(organization));
		return "org/editTrench";
	}
	
	/**
	 * 根据orgId查询单条渠道记录
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/trenchByIdDetails")
	public String trenchByIdDetails(Model model,Organization organization)
	{
		model.addAttribute("org",OrganizationService.selectByPrimaryKey(organization));
		return "org/trenchDetails";
	}
	
	/**
	 * 根据orgId查询单条物业记录
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/selectTenementById")
	public String selectTenementById(Model model,Organization organization)
	{
		model.addAttribute("org",OrganizationService.selectTrench(organization));
		return "org/editTenement";
	}
	
	/**
	 * 根据orgId查询单条物业记录
	 * @param model
	 * @param organization
	 * @return
	 */
	@RequestMapping("/tenementByIdDetails")
	public String tenementByIdDetails(Model model,Organization organization)
	{
		model.addAttribute("org",OrganizationService.selectTrench(organization));
		return "org/tenementDetails";
	}
	
	/**
	 * 根据orgId修改单条记录
	 * @param model
	 * @param organization
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="update",method = RequestMethod.POST)
	public int update(@CurrentUser User user,Model model,Organization organization)
	{
		return OrganizationService.update(user,organization);
	}
	
	/**
	 * 分页查询,json返回
	 * 
	 * @param Organization
	 * @param page
	 * @param model
	 * @return
	 */
	// @RequiresPermissions("user:view")
	@ResponseBody
	@RequestMapping(value = "listJson", method = RequestMethod.GET)
	public PageReturn<Organization> listJson(Organization org, Page page, Model model) {
		if(org.getOrgType() != null && org.getOrgType()==12){ //平台
			org.setOrgType(1);
		}if(org.getOrgType() != null && org.getOrgType()==13){   //渠道
			org.setOrgType(2);
		}if(org.getOrgType() != null && org.getOrgType()==14){    //物业
			org.setOrgType(3);
		}
		return OrganizationService.selectRelationForPage(org, page);
	}
	
	
	  /**
	   * 根据id字符串查询
	  * 
	  * @param ids
	  *  逗号分隔
	  * @return
	  */
	 @ResponseBody
	 @RequestMapping(value = "/{ids}/selectByIds", method = RequestMethod.GET)
	 public List<Organization> selectByIds(@PathVariable("ids") String ids) {
		return OrganizationService.selectByIds(ids);
	}
	 
	 @RequestMapping("/{isMultiple}/orgSearch")
	 public String orgSearch(@PathVariable("isMultiple") String isMultiple,Organization org, Model model)
	 {
		 model.addAttribute("isMultiple", isMultiple);
		 return "org/orgSearch";
	 }
	 
	/**
	 * 禁用启用
	 * 
	 * @param organization
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "updateByAuditStatus", method = RequestMethod.POST)
	public Integer updateByAuditStatus(Organization organization) {
		return OrganizationService.updateByAuditStatus(organization);
	}
	
	/**
	 * 转向新增页面
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/{id}/userCreate", method = RequestMethod.GET)
	public String userCreate(@PathVariable("id") Long id,Model model) {
		User user = new User();
		Organization org = new Organization();
		org.setOrgId(id);
		List<Organization> list = OrganizationService.findOrgList(org);
		if(list != null && list.size() > 0){
			org = list.get(0);
			user.setOrgId(org.getOrgId());
			user.setUseRealName(org.getContactName());
			user.setUseMobilePhone(String.valueOf(org.getContactNumber()));
			user.setOrg(org);
		}
		model.addAttribute("user", user);
		model.addAttribute("op", "新增用户");
		return "sys/userEdit";
	}
	
	/**
	 * 新增用户
	 * 
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/{id}/userCreate", method = RequestMethod.POST)
	public String createUser(@CurrentUser User currUser, User user) {
		user.setInsertBy(currUser.getUseId());
		return service.insert(user);
	}
	 
}
